﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

namespace com.tiyma.web
{
    public class BasePage : Page
    {

        /// <summary>
        /// 获取分页HTML
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="pageUrl"></param>
        /// <returns></returns>
        protected string GetPageHtml(int pageIndex, int pageSize, int count, string pageUrl)
        {

            StringBuilder sb = new StringBuilder();
     
            int pageCount = (int)Math.Ceiling((double)count / pageSize);
            int start = ((pageIndex - 1) / 10) * 10 + 1;
            for (int i = start; i <= pageCount && i < start + 10; i++)
            {
                if (i == pageIndex)
                    sb.AppendFormat("<span title=\"当前第{0}页\">{1}</span>", i, i);
                else
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"第{1}页\">{2}</a></span>", pageUrl + i, i, i);
            }
            if (start > 1)
            {
                sb.Insert(0, String.Format("<span><a href=\"{0}\" title=\"第{1}页\">...</a></span>", pageUrl + (start - 1), (start - 1)));
                sb.Insert(0, String.Format("<span><a href=\"{0}\" title=\"首页\">{1}</a></span>", pageUrl + 1, 1));
            }
            if (start + 10 <= pageCount)
            {
                sb.AppendFormat("<span><a href=\"{0}\" title=\"第{1}页\">...</a></span>", pageUrl + (start + 10), start + 10);
                sb.AppendFormat("<span><a href=\"{0}\" title=\"第{1}页(末页)\">{2}</a></span>", pageUrl + pageCount, pageCount, pageCount);
            }

            if (pageCount > 1)
            {
                sb.AppendFormat("<span><a href=\"{0}\" title=\"第{1}页\">下一页</a></span>", pageUrl + (pageIndex + 1),  pageIndex + 1);
            }
            sb.Insert(0, "<div id=\"pagediv\" class=\"page\">");
            sb.Append("</div>");

            return sb.ToString();
        }

    }
}
